#!/bin/bash
# wait-for-db.sh

set -e

host="$1"
shift
cmd="$@"

until PGPASSWORD=testpass psql -h "$host" -U testuser -d testdb -c '\q' >/dev/null 2>&1; do
  echo "⏳ PostgreSQL is unavailable - sleeping, Waiting for PostgreSQL to be ready..."
  sleep 2
done

echo "✅ PostgreSQL is up - executing command:  $cmd"
exec $cmd
